
*** Table 3 Simulated impacts, compare cap and trade : direct or indirect energy intensities



* COLUMNS ARE: TAX ONLY ; TAX + TRADE-BASED SUBSIDY ; TAX + NET TRADE-BASED SUBSIDY  ; TAX + CONSUMPTION BASED SUBSIDY (5 COLUMNS INCL COLUMN DEFINING VARIABLE TO BE SUMMARIZED) 

************************************
* READ Variables
************************************
local filename "$filename"
local num1 $num1
local num2 $num2
local num3 $num3
************************************



file open Table using "$tables/`filename'.tex", write replace


* table head
file write Table "\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} p{1.6in} c c c c c}" _n
file write Table "\toprule " _n
file write Table "          &       Carbon Tax   & \multicolumn{3}{c}{CAT and Subsidy}  \\   \cline{3-5} " _n
file write Table "          &      & Baseline     & Alternative 1    &     Alternative 2  \\ " _n
file write Table "          &   (USD \\$25)   &  (USD \\$`num1')   &  (USD \\$`num2')         &    (USD \\$`num3')  \\ " _n
file write Table "          &    (1)  & (2)    & (3)     &    (4) \\ " _n
file write Table " \midrule " _n


* TOP PANEL OF THE TABLE REPORTS PC CHANGE IN PRODUCTION, IMPORTS, EXPORTS, NET IMPORTS. REPORT MEDIAN (5 PC 95 PC)

file write Table "\multicolumn{5}{c}{\textbf{A: Impacts on Domestic Shipments, Imports, Exports (\%)}} \\[2ex] "_n
*file write Table "\midrule " _n

* statisitcs:
foreach yvar in "prod"  "exp"  "imp" {

    * (1) TAX
		
        * extract variable label
	    local label: variable label pc_`yvar'_t

		* stats
	    quietly sum pc_`yvar'_t, detail
		local p5_tax  = r(p5)
		local p50_tax = r(p50)
		local p95_tax = r(p95)
		

	* (2) TAX+subsidy	* (2) TAX+subsidy
	local i = 0
    foreach sub in  $caselist  {
    
		* get the corresponding tax:
		local num = ""
		if "`sub'"!="t"{
			local num = "_`num`i''"
		}
		local i = `i' + 1
	
		* stats
	    quietly sum pc_`yvar'`num'_`sub', detail
		
		local p5_`sub'  = r(p5)
		local p50_`sub' = r(p50)
		local p95_`sub' = r(p95)

	}
	
	* (3) write one line
	file write Table "`label' "	
	foreach sub in $caselist {
		file write Table " & $" _tab %7.1f (`p50_`sub'')  "$" 
	}
	file write Table " \\ " _n
	foreach sub in  $caselist {
		file write Table " &" _tab  "$[" %7.1f (`p5_`sub'') "," %7.1f (`p95_`sub'') "]$" 
	}
	file write Table " \\[0.5em]" _n
	
	
}
*


* Bottom PANEL REPORTS ABATEMENT, LEAKAGE_T, LEAKAGE_C, NET ABATEMENT PC _T NET ABATEMENT PC _C
file write Table "\midrule " _n
file write Table "\multicolumn{5}{c}{\textbf{B: Annual Emission Abatement and Emissions Leakage (\%)}} \\[2ex] "_n

* statisitcs:
* write one line with prices
foreach var in "abaterate"  "netabrate"  "leakagerate"  "tax_rev_agg"  "tr_agg"  "num_industries" {

    * (1) TAX
		
        * extract variable label
	    local label: variable label `var'_t
		
		* stats: trade 
	    quietly sum `var'_t, detail
		
		local p5_tax  = r(p5)
		local p50_tax = r(p50)
		local p95_tax = r(p95)
		
	* (2) TAX+subsidy	* (2) TAX+subsidy
	local i = 0
	foreach sub in  $caselist  {
    
		* get the corresponding tax:
		local num = ""
		if "`sub'"!="t"{
			local num = "_`num`i''"
		}
		local i = `i' + 1
	
		* stats
	    quietly sum `var'`num'_`sub', detail
		
		local p5_`sub'  = r(p5)
		local p50_`sub' = r(p50)
		local p95_`sub' = r(p95)

	}
	
	* (3) write one line
	if "`var'" != "num_industries" &  "`var'" != "tr_agg" &  "`var'" != "leakagerate"  {
		file write Table "`label' "
		foreach sub in $caselist {
			file write Table " & $" _tab %7.1f (`p50_`sub'') "$"
		}
		file write Table 	" \\ " _n
		foreach sub in  $caselist {
			file write Table " &" _tab  "$[" %7.1f (`p5_`sub'') "," %7.1f (`p95_`sub'') "]$" 
		}
		file write Table " \\[0.5em]" _n
	}
	else if ("`var'" == "leakagerate") {
		file write Table "`label' "
		foreach sub in $caselist {
			file write Table " & $" _tab %7.2f (`p50_`sub'') "$"
		}
		file write Table 	" \\ " _n
		foreach sub in  $caselist {
			file write Table " &" _tab  "$[" %7.2f (`p5_`sub'') "," %7.2f (`p95_`sub'') "]$" 
		}
		file write Table " \\[0.5em]" _n
	}
	else if ( "`var'" == "tr_agg") {
		file write Table "`label' "
		foreach sub in $caselist {
			file write Table " & $" _tab %7.2f (`p50_`sub'') "$"
		}
		file write Table " \\[0.5em]" _n
	}
	else {
		file write Table "`label' "
		foreach sub in $caselist {
			file write Table " & $" _tab %7.0f (`p50_`sub'') "$"
		}
		file write Table " \\[0.5em]" _n
	}
	
}
*


* end of table
file write Table "\bottomrule " _n
file write Table "\end{tabular*} " _n

* footnote
*file write Table "\vspace{-1ex} " _n
*file write Table "\begin{minipage}{\textwidth} " _n
*file write Table "{\footnotesize \textit{Notes:} All values deflated to 2007 U.S. dollars.} "_n
*file write Table "\end{minipage} " _n


* close file
file close Table
